package com.future.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.domain.Article;
import com.future.domain.DTO.RegisterUserDTO;
import com.future.domain.DTO.StudentClassDTO;
import com.future.domain.DTO.UserDTO;
import com.future.domain.Result;
import com.future.domain.User;
import com.future.mapper.UserMapper;
import com.future.service.AdminService;
import com.future.allUtils.constants.PermissionConstants;
import com.future.service.MediaResourceService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private MediaResourceService mediaResourceService;

    /**
     * 教师查询班级学生信息
     * 只能查询/管理自己班级的学生
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.TEACHER_STUDENT_MANAGE + "','" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @GetMapping("/class/{classId}")
    public Result getClassStudents(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @PathVariable Long classId) {
        return adminService.getClassStudents(pageNum, pageSize, classId);
    }
    /**
     * 教师禁言/解禁班级学生
     */

    @PreAuthorize("hasAuthority('" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @PutMapping("/class/student/status")
    public Result updateClassStudentStatus(@RequestParam Long studentId, @RequestParam Integer status) {
        // 实现更新班级学生状态逻辑
        return adminService.updateClassStudentStatus(studentId, status);
    }
    /**
     * 教师管理班级学生信息
     */
    @PreAuthorize("hasAuthority('" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @PostMapping("/class/student")
    public Result updateClassStudent(@RequestBody User user) {
        // 实现更新班级学生信息逻辑
        return adminService.updateClassStudent(user);
    }

    /**
     * 超级管理员查询所有用户信息
     */
    @PreAuthorize("hasAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @GetMapping("/all")
    public Result getAllUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // 实现获取所有用户列表（分页）逻辑
        return adminService.getAllUsers(pageNum, pageSize);
    }
    /**
     * 管理员禁言/解禁用户
     */
    @ApiOperation("管理员禁言/解禁用户")
    @PreAuthorize("hasAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @PostMapping("/update/status")
    Result updateUserStatus(@RequestParam Long userId, @RequestParam Integer status){
        return adminService.updateUserStatus(userId, status);
    }
    /**
     * 管理员更新用户信息
     */
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    Result updateUser(@RequestBody User user){
        return adminService.updateUser(user);
    }
 /**
 * 管理员审核用户认证
 * @param certificationId 认证ID
 * @param status 审核状态（0：拒绝，1：通过）
 * @param rejectReason 拒绝原因（可选）
 * @return 操作结果
 */
 @PreAuthorize("hasAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
 @PostMapping("/certifications/{certificationId}/audit")
 public Result auditCertification(
         @PathVariable Long certificationId,
         @RequestParam Integer status,
         @RequestParam(required = false) String rejectReason) {
     return adminService.auditCertification(certificationId, status, rejectReason);
 }
     /**
     * 管理员查询角色申请待审核认证申请列表
     */
    @PreAuthorize("hasAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @GetMapping("/certifications/pending")
    @ApiOperation("查询待审核认证申请")
    public Result getPendingCertifications(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return adminService.getPendingCertifications(pageNum, pageSize);
    }
    /**
     * 审核学生加入班级申请
     * @param applyId 申请记录ID
     * @param status 审核状态：0-拒绝 ，1-通过
     * @param rejectReason 拒绝原因（状态为拒绝时必填）
     * @return 操作结果
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "', '" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @PostMapping("/class/apply/audit")
    public Result auditStudentJoinClassApply(Long applyId, Integer status, String rejectReason) {
        return adminService.auditStudentJoinClassApply(applyId, status, rejectReason);
    }
    /**
     * 审核学生退出班级申请
     * @param applyId 申请记录ID
     * @param status 审核状态：0-拒绝 ，1-通过
     * @param rejectReason 拒绝原因（状态为拒绝时必填）
     * @return 操作结果
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "', '" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @PostMapping("/class/apply/quit/audit")
    public Result auditStudentQuitClassApply(Long applyId, Integer status, String rejectReason) {
        return adminService.auditStudentQuitClassApply(applyId, status, rejectReason);
    }
    /**
     * 查询待审核班级申请列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 待审核加入班级申请列表
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "', '" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @GetMapping("/class/apply/pending")
    @ApiOperation("查询待审核班级申请")
    public Result getPendingStudentClassApplications(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return adminService.getPendingStudentClassApplications(pageNum, pageSize);
    }

    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "', '" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
    @ApiOperation(value = "创建班级")
    @PostMapping("/contributeClass")
    public Result contributeClass (StudentClassDTO dto){
        return adminService.contributeClass(dto);
    }

    @ApiOperation(value = "根据用户ID获取班级ID")
    @GetMapping("/classIdByUserId")
    public Long getClassIdByUserId(@RequestParam Long userId){
        return adminService.getClassIdByUserId(userId);
    }

    @ApiOperation(value = "根据班级ID获取班级人数")
    @GetMapping("/classNumByClassId")
    public Result getClassNumByClassId(@RequestParam Long classId){
        return adminService.getClassNumByClassId(classId);
    }

    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @ApiOperation(value = "管理员添加学生账号")
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody RegisterUserDTO dto){
        return adminService.addStudent(dto);
    }

    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_USER_MANAGE + "')")
    @ApiOperation("查询学生")
    @GetMapping("/getStudent")
    public Result getStudent(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        return adminService.getStudent(pageNum, pageSize);
    }

    @ApiOperation("管理员审核媒体资源")
    @PutMapping("/approve/{id}")
    public Result approveMedia(@PathVariable Long id, @RequestParam Integer approved, @RequestParam String reason) {
        return mediaResourceService.approveMedia(id, approved, reason);
    }
    @ApiOperation("管理员获取待审核媒体资源")
    @GetMapping("/pending")
    public Result getPendingMedia(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        return mediaResourceService.getPendingMedia(pageNum, pageSize);
    }

}